package com.nyist.diseasepreventionserver.model.Identity;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Auther: 杜習田
 * @Date: 2020/2/28 10:13
 * @Description:
 */
@Service
public class KeepIdentityServiceImpl<T> implements KeepIdentityService<T> {

    @Override
    public T setIdentity(String identityString, T object, HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute(identityString,object);
        return null;
    }

    @Override
    public T getIdentity(String identityString, HttpServletRequest request) {
        T object = (T)request.getSession().getAttribute(identityString);
        return object;
    }

    @Override
    public T delIdentity(String identityString, HttpServletRequest request) {
        T object = (T)request.getSession().getAttribute(identityString);
        request.getSession().removeAttribute(identityString);
        return object;
    }

    @Override
    public Boolean isIdentity(String identityString, HttpServletRequest request) {
        T object = (T)request.getSession().getAttribute(identityString);
        if(StringUtils.isEmpty(object)){
            return false;
        }else
            return true;
    }
}
